<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreActive;
use App\Http\Requests\StoreGood;
use App\Http\Requests\UpdateActive;
use App\Http\Requests\UpdateGood;
use App\Models\Active;
use App\Models\ActiveOncustomer;
use App\Models\Coupon;
use App\Models\Good;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class GoodController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_goods' => 'am-in',
            '_good' => 'am-active',
        ]);

    }


    // 通知活动首页
    public function index()
    {
        $goods = Good::with('store')->paginate(20);

        return view('admin.good.index',compact('goods'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates = Store::all();

        return view('admin.good.create', compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreGood $request)
    {

        $info = $request->all();
        $good = Good::create($info);
        if (!file_exists(public_path('qrcodes')))
            mkdir(public_path('qrcodes'));
        QrCode::format('png')->size(200)->generate('good,'.$good['id'], public_path('qrcodes/' . $good['id'] . '.png'));
        $data['img'] = 'qrcodes/' . $good['id'] . '.png';
        $good->update($data);
        return redirect('admin/good')->with('suceess', '新增成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cates = Store::all();

        $good = Good::find($id);
        return view('admin.good.edit', compact('cates', 'good'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateGood $request, $id)
    {
        $good = Good::find($id);

        $good->update($request->all());
        return redirect('admin/good')->with('success', '修改成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Good::destroy($id);
        return back()->with('success','删除成功');

    }
}
